package com.eostoken.eos_wallet.mvp.mode;

import android.app.Activity;
import android.content.Context;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.eostoken.eos_wallet.bean.AccountInfo;
import com.eostoken.eos_wallet.eosnet.common.EOSNetWorkManager;
import com.eostoken.eos_wallet.eosnet.common.Params;
import com.eostoken.eos_wallet.mvp.callback.CommonCallback;
import com.eostoken.eos_wallet.net.common.RxSubscriber;
import com.eostoken.eos_wallet.utils.Constants;
import com.fredy.mvp.Model;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.eostoken.eos_wallet.utils.Constants.ACCOUNT_INFO;

public class ResourceAdministrationModel implements Model {

    public void getAccount(Activity context, Map<String, Object> map, boolean showLoading, CommonCallback callback) {
        Observable.fromCallable(() -> EOSNetWorkManager.getRpcProvider().getAccount(Params.getRequestBody(map)))
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new RxSubscriber<String>(context, showLoading) {

                    @Override
                    public void onFail(String failMsg) {
                        LogUtils.dTag(Constants.TAG_MGP, "getAccount onFail: " + failMsg);
                        callback.onFailure(failMsg, ACCOUNT_INFO);
                    }

                    @Override
                    public void onSuccess(String s) {
                        LogUtils.dTag(Constants.TAG_MGP, "getAccount onSuccess: " + s);
                        AccountInfo accountInfo = GsonUtils.fromJson(s, AccountInfo.class);
                        callback.onSuccess(accountInfo, "", ACCOUNT_INFO);
                    }
                });
    }

}
